import { Component, OnInit } from '@angular/core';
import * as constants from '../../../providers/Constants'
import { NavController, AlertController } from '@ionic/angular';
import { ToastService } from './../../../providers/toast/toast.service'
import { Storage } from 'src/app/providers/Storage';
import { AuthService } from 'src/app/services/auth.service';
import { Utils } from 'src/app/providers/Utils';
ToastService
@Component({
  selector: 'app-forget-password',
  templateUrl: './forget-password.page.html',
  styleUrls: ['./forget-password.page.scss'],
})
export class ForgetPasswordPage implements OnInit {
  newPswStr: string = '';
  codeStr: string = '';
  emailStr: string = '';
    // 验证码倒计时
    verifyCode: any = {
      verifyCodeTips: "Get code",
      countdown: constants.DEFAULT_SMS_TIME,
      disable: true
    }
  constructor(private toast: ToastService,
    private alertController: AlertController,
    public nav: NavController,
    public service: AuthService, ) {

  }
  ngOnInit() {
  }

  onBtn() {
    if (!this.emailStr) {
      this.toast.show('Please enter your email address');
    } else if (!this.codeStr) {
      this.toast.show('Please enter the verification code');
    } else if (!this.newPswStr) {
      this.toast.show('enter your new password');
    } else if (this.newPswStr.length < 6) {
      this.toast.show('incorreCt password');
    } else {
      let data = {
        userPhone: this.emailStr,
        userPassWord: this.newPswStr,
        code: this.codeStr,
        email: this.codeStr,
        type: 1,
      }
      this.service.forgetPassword(data).subscribe(res => {
        // 保存成功返沪上一页，否则弹出提示框
        if (res.retCode == '000000') {
          this.nav.navigateRoot('/login');
          // this.getUserInfo();
        } else if (constants.errCode[res.retCode]) {
          if (constants.errCode[res.retCode] != '0') {
            this.toast.show(constants.errCode[res.retCode]);
          }
        } else if (res.retMsg) {
          this.toast.show(res.retMsg);
        } else {
          this.toast.show("exception service routine");
        }
      });
    }
  }



  getSms() {
    if (!this.emailStr) {
      this.toast.show('Please enter your email address');
    } else if (!Utils.isEmail(this.emailStr)) {
      this.toast.show('Please enter your email address');
    } else {
      if (this.verifyCode.disable) {
        let data = {
          email: this.emailStr,
          type: 1,
        }
        this.service.getSms(data).subscribe(res => {
          // 保存成功返沪上一页，否则弹出提示框
          if (res.retCode == '000000') {
            this.toast.show("send successfully");
            this.settime();
          } else {
            if (constants.errCode[res.retCode]) {
              if (constants.errCode[res.retCode] != '0') { this.toast.show(constants.errCode[res.retCode]); }
            } else if (res.retMsg) {
              this.toast.show(res.retMsg);
            } else {
              this.toast.show("error");
            }
          }
        });
      }
    }
  }
  // 倒计时
  settime() {
    if (this.verifyCode.countdown == 1) {
      this.verifyCode.countdown = constants.DEFAULT_SMS_TIME;
      this.verifyCode.verifyCodeTips = "Get code";
      this.verifyCode.disable = true;
      return;
    } else {
      this.verifyCode.disable = false;
      this.verifyCode.countdown--;
    }
    this.verifyCode.verifyCodeTips = "Get (" + this.verifyCode.countdown + ")";
    setTimeout(() => {
      this.verifyCode.verifyCodeTips = "Get (" + this.verifyCode.countdown + ")";
      this.settime();
    }, 1000);
  }

}
